package com.eagletsoft.mmproj.service;

import java.io.IOException;

import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.net.RequestListener;

public abstract class BaseService implements RequestListener{
	
	protected String reqType;
	protected IServiceListener listener;
	public BaseService(String reqType, IServiceListener listener) {
		super();
		this.reqType = reqType;
		this.listener = listener;
	}
	
	@Override
	public void onComplete(String res) {
		try
		{
			ServiceResult sr = makeResult(res);
			listener.callback(reqType, sr); 
		}
		catch (Exception ex)
		{
			ServiceResult sr = new ServiceResult(ServiceResult.UNKNOWNERROR, ex.getMessage(), null);
			listener.callback(reqType, sr);
		}
	}

	abstract protected ServiceResult makeResult(String res) throws Exception;
	
	@Override
	public void onError(WeiboException ex) {
		ServiceResult sr = new ServiceResult(ex.getStatusCode(), ex.getMessage(), null);
		listener.callback(reqType, sr);
	}

	@Override
	public void onIOException(IOException ex) {
		ServiceResult sr = new ServiceResult(ServiceResult.IOERROR, ex.getMessage(), null);
		listener.callback(reqType, sr);
	}
}
